var express = require('express');
var http = require('http');
var WebSocket = require('ws');

var app = express();
app.use(express.static(__dirname));

var server = http.createServer(app);
var wss = new WebSocket.Server({server});

wss.on('connection', function connection(ws) {
    console.log('链接成功！');
    ws.on('message', function (data) {
        data = JSON.parse(data);
        console.log(data)
        switch (data.cmd) {
            case 'getPrinters':
                ws.send(JSON.stringify({
                    "cmd":"getPrinters",
                    "defaultPrinter":"Microsoft XPS Document Writer",
                    "msg":"成功",
                    "printers":[
                        {
                            "name":"KM-118",
                            "status":"enable",
                            "type":"other"
                        },
                        {
                            "name":"Microsoft XPS Document Writer",
                            "status":"enable",
                            "type":"other"
                        }
                    ],
                    "requestID":data.requestID,
                    "status":"success"
                }))
                break;
            case 'getAgentInfo':
                ws.send(JSON.stringify({
                    "cmd":"getAgentInfo",
                    "msg":"成功",
                    "requestID":data.requestID,
                    "status":"success",
                    "version":"0.4.8.6"
                }))
                break;
            case 'setPrinterConfig':
                ws.send(JSON.stringify({
                    "cmd":"setPrinterConfig",
                    "msg":"成功",
                    "requestID":data.requestID,
                    "status":"success"
                }))
                break;
            case 'print':
                ws.send(JSON.stringify({
                    "cmd":"print",
                    "msg":"成功",
                    "requestID":data.requestID,
                    "status":"success",
                    "taskID":data.task.taskID
                }))
                ws.send(JSON.stringify({
                    "cmd":"notifyPrintResult",
                    "printStatus":[
                        {
                            "detail":"",
                            "documentID":"1/1|00554A06|596648868634926497|ZJS000075031353",
                            "msg":"成功",
                            "printer":"Microsoft XPS Document Writer",
                            "status":"success"
                        }
                    ],
                    "printer":"Microsoft XPS Document Writer",
                    "requestID":data.requestID,
                    "taskID":data.task.taskID,
                    "taskStatus":"rendered"
                }))
                ws.send(JSON.stringify({
                    "cmd":"notifyPrintResult",
                    "errorCode":23,
                    "msg":"打印任务错误",
                    "printStatus":[
                        {
                            "detail":"",
                            "documentID":"1/1|00554A06|596648868634926497|ZJS000075031353",
                            "msg":"文档被取消",
                            "printer":"Microsoft XPS Document Writer",
                            "status":"canceled"
                        }
                    ],
                    "printer":"Microsoft XPS Document Writer",
                    "requestID":data.requestID,
                    "taskID":data.task.taskID,
                    "taskStatus":"failed"
                }))
                break;
             
            default:
                //ws.send()
                break;
        }
       
    });
});

server.listen(13528, function listening() {
    console.log('服务器启动成功！');
});
